﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdSaveFile.h"
#include "CommandIds.h"
#include "CommandRegister.h"
#include "IAction.h"
#include "IDocument.h"
#include "UiCommonDialog.h"
#include "GbmpFileSystem.h"
#include "NotificationUtils.h"

#include "UiDocumentViewUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

using namespace Sample;
using namespace gcmp;

REGISTER_COMMAND(CmdSaveFile)

CmdSaveFile::CmdSaveFile() 
    : CommandBase(ID_CMD_SAVE_FILE, true)
{
}

bool CmdSaveFile::IsVisible() const
{
    return UiDocumentViewUtils::GetCurrentDocument() != nullptr;
}

gcmp::OwnerPtr<gcmp::IAction> CmdSaveFile::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    gcmp::IDocument* pDocument = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDocument, L"UI文档获取失败",L"GDMP", L"2023-09-30");
    if (!pDocument->GetWasSaved())
    {
        ICommandManager::Get()->SendCommand(ID_CMD_SAVE_FILE_AS);
    }
    else
    {
        DocumentSaveResult docSaveResult;
        DocumentSaveOptions docSaveOptions(false);
        if (!pDocument->Save(docSaveOptions, docSaveResult))
        {
            std::wstring messageBox = GBMP_TR(L"未知错误！");
            switch (docSaveResult.Code)
            {
            case DocumentSaveError::FilePathNotExist:
                messageBox = GBMP_TR(L"文件路径不存在！");
                break;
            case DocumentSaveError::InvalidFilePath:
                messageBox = GBMP_TR(L"无效的文件路径！");
                break;
            case DocumentSaveError::CanNotSaveMaterials:
                messageBox = GBMP_TR(L"无法保存材质文件！");
                break;
            default:
                break;
            }
            //强提示
            UiCommonDialog::ShowMessageBox(GBMP_TR(L"GDMP"),
                messageBox, (int)UiCommonDialog::ButtonType::OK);
        }
        else
        {   
            //弱提示
            NotificationUtils::PostNotification(NotificationTypes::SuccessNotificationType,
                GBMP_TR(L"文件保存成功！"));
        }
    }
    return nullptr;
}